﻿/*
 * OpenDoPE Word Add-In
 * Copyright (C) 2010-2011  Plutext Pty Ltd
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * 
 * For details, see http://www.gnu.org/licenses/gpl-2.0.html
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Office = Microsoft.Office.Core;
using NLog;

namespace OpenDoPE_Wed
{
    public class CustomXmlUtilities
    {

        static Logger log = LogManager.GetLogger("OpenDoPE_Wed");

        public static void replaceXmlDoc(Office.CustomXMLPart cxp, String newContent)
        {
            /* Office.CustomXMLNode node = cxp.SelectSingleNode("/");
            // gives you #document,
            // but you can't get the root node from it?

           //node = cxp.SelectSingleNode("/mypart");
             */

            Office.CustomXMLNode node = cxp.SelectSingleNode("/node()");
            //log.Debug(node.XML);
            //log.Debug(node.XPath);

            Office.CustomXMLNode parent = node.ParentNode;

            log.Debug(parent.BaseName);

            //parent.ReplaceChildNode(node, "mynewnode", "", Office.MsoCustomXMLNodeType.msoCustomXMLNodeElement, "");

            parent.ReplaceChildSubtree(newContent, node);

            //parent.RemoveChild(node); //doesn't work

            //parent.AppendChildSubtree("<mynewpart><blagh/></mynewpart>");

            node = cxp.SelectSingleNode("/");
            log.Debug(node.XML);

        }

    }
}
